<?php
class WidgetLayoutBase extends CComponent
{
    private $_widget;
    private $_c;
    
    public $info=array(
        'path'=>'',
        'name'=>'',
        'description'=>'',
        'images'=>'',
        'author'=>'',
        'version'=>1,
    );
    
    /**
    * 
    * @param ContentBaseController $widget
    */
    public function __construct($widget)
    {
        $this->_widget = $widget;
        $this->getConfigParams();
    }
    
    public function getConfigParams()
    {
        if ($this->_c === null)
        {
            //load data from db
            $criteria = new CDbCriteria;
            $criteria->with = array('params');
            $criteria->compare('path', $this->info['path']);
            $widgetLayout = CmsWidgetLayout::model()->find($criteria);
            $widgetLayoutParams = array();
            $widgetLayoutSettings = array();
            if (is_object($widgetLayout))
            {
                //load widget_layout_settings from page_widget
                if (!is_object(ContentBaseController::$pageWidget) && isset($_GET['page_widget_id']))
                {
                    ContentBaseController::$pageWidget = PageWidget::model()->findByPk($_GET['page_widget_id']);
                }
                if (is_object(ContentBaseController::$pageWidget))
                {
                    if (is_array(ContentBaseController::$pageWidget->widget_settings))
                        $widgetLayoutSettings = ContentBaseController::$pageWidget->widget_settings;
                    elseif (is_string(ContentBaseController::$pageWidget->widget_settings))
                        $widgetLayoutSettings = $this->evaluateExpression(ContentBaseController::$pageWidget->widget_settings);
                    if (isset($widgetLayoutSettings['layout']))
                        $widgetLayoutSettings = $widgetLayoutSettings['layout'];
                    else
                        $widgetLayoutSettings = array();
                }
                else
                    $widgetLayoutParams = $widgetLayout->params;
            }
            //merge data
            $defaultConfig = $this->getDefaultConfig();
            if (is_array($defaultConfig))
            {
                //widget layout settings of page_widget
                if (is_array($widgetLayoutSettings) && count($widgetLayoutSettings))
                {
                    foreach ($widgetLayoutSettings as $key => $data)
                    {
                        if (isset($defaultConfig[$key]))
                            $defaultConfig[$key]['value'] = $data;
                        else//extra config, not in default widget layout config
                            $defaultConfig[$key] = array(
                                'value'=>$data,
                                'visible'=>0,
                            );
                    }
                }
                elseif (is_array($widgetLayoutParams) && count($widgetLayoutParams))
                {
                    foreach ($widgetLayoutParams as $widgetLayoutParam)
                    {
                        if (isset($defaultConfig[$widgetLayoutParam->name]))
                        {
                            $data = array(
                                'label'=>'',
                                'value'=>'',
                                'description'=>'',
                                'setting_group'=>'',
                                'ordering'=>'',
                                'visible'=>'',
                            );
                            $data = array_intersect_key($widgetLayoutParam->attributes, $data);
                            $defaultConfig[$widgetLayoutParam->name] = CMap::mergeArray($defaultConfig[$widgetLayoutParam->name], $data);
                        }
                    }
                }
            }
            $this->_c = $defaultConfig;
        }
        return $this->_c;
    }
    
    /**
    * <pre>
    * $configValue=$layout->configName;
    * </pre>
    * 
    * @param mixed $name
    */
    public function __get($name)
    {
        if(is_array($this->_c) && isset($this->_c[$name]))
        {
            if(isset($this->_c[$name], $this->_c[$name]['value']))
                return $this->_c[$name]['value'];
            else
                return null;
        }
        else
            return parent::__get($name);
    }
    
    /**
    * <pre>
    * $this->configName=$configValue;
    * </pre>
    * 
    * @param mixed $name
    */
    public function __set($name,$value)
    {
        if(is_array($this->_c) && isset($this->_c[$name]))
        {
            $this->_c[$name]['value'] = $value;
        }
        else
            parent::__set($name);
    }
    
    public function getViewFile()
    {
        //load view file from config
        $layoutFolder = get_class($this);
        $layoutFolder = str_replace('Layout', '', $layoutFolder);
        $layoutFolder = str_replace(ucfirst(substr(strrchr($this->_widget->getId(), "/"), 1)), '', $layoutFolder);
        if (strlen($layoutFolder) > 0)
            $layoutFolder[0] = strtolower($layoutFolder[0]);
        return $layoutFolder.'/'.$this->view;
    }
    
    /**
    * 
    * @return array
    * 
    * 'name' => array(
                'type' => CHtml::function or Yii path to a widget. 
                            If CHtml function then the function name is in lower case. 
                            Suffixes like 'field' or 'button' must be removed
                'rules' => array('rule name - see Yii rule classes' => array(config)),
                'items' => array(key => value) //used for dropDownList of similar CHtml functions
                'htmlOptions' => array()
                // following items are used to setup the widget setting info while installing  
                'label'           => 'Short friendly param name'
                'value'           => 'default value'
                'description'     => 'Full description'
                'setting_group'   => 'General Settings'
                'ordering'        => 0
                'visible'         => 0
            ),
    */
    public function getDefaultConfig()
    {
        return array(
            'view'=>array(
                'label'=>'View file',
                'value'=>'view',
                'description'=>'View file',
                'setting_group'=>'General Settings',
                'type'=>'dropdownlist',
                'items'=>array('view'=>'Default'),
                'visible'=>true,
            ),
        );
    }
    
    public function install()
    {
        $widgetLayout = new CmsWidgetLayout;
        $widgetLayout->attributes = $this->info;
        $widgetLayout->status = true;
        $widgetLayout->widget_id = $this->_widget->id;
        if ($widgetLayout->save())
        {
            //insert default settings
            $defaultConfig = $this->getDefaultConfig();
            foreach ($defaultConfig as $name => $config)
            {
                $model = new WidgetLayoutSetting;
                $model->label = $config['label'];
                $model->value = $config['value'];
                $model->description = $config['description'];
                $model->setting_group = $config['setting_group'];
                $model->visible = $config['visible'];
                $model->name = $name;
                $model->widget_layout_id = $widgetLayout->id;
                if (!$model->save())
                {
                    Yii::log(CVarDumper::dumpAsString($model->getErrors()), CLogger::LEVEL_ERROR, 'CmsWidget.install');
                }
            }
        }
        else
        {
            Yii::log(CVarDumper::dumpAsString($widgetLayout->getErrors()), Clogger::LEVEL_ERROR, 'CmsWidget.install');
        }
    }
}
